package com.ruoyi.pims.controller;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pims.domain.HealthBasic;
import com.ruoyi.pims.service.IHealthBasicService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 健康基础数据Controller
 * 
 * @author ruoyi
 * @date 2025-05-16
 */
@Controller
@RequestMapping("/pims/health_basic")
public class HealthBasicController extends BaseController
{
    private String prefix = "pims/health_basic";

    @Autowired
    private IHealthBasicService healthBasicService;
    @Autowired
    private ISysRoleService sysRoleService;


    @RequiresPermissions("pims:health_basic:view")
    @GetMapping()
    public String health_basic()
    {
        return prefix + "/health_basic";
    }

    /**
     * 查询健康基础数据列表
     */
    @RequiresPermissions("pims:health_basic:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HealthBasic healthBasic)
    {
        startPage();
        Set<String> roles =sysRoleService.selectRoleKeys(ShiroUtils.getUserId());
        logger.debug("查询当前用户的角色列表");
        logger.debug(roles.toString());
        boolean isAdmin = roles.contains("damin");
        if (!isAdmin){
            healthBasic.setUserId(ShiroUtils.getUserId());
        }

        List<HealthBasic> list = healthBasicService.selectHealthBasicList(healthBasic);
        return getDataTable(list);
    }

    /**
     * 导出健康基础数据列表
     */
    @RequiresPermissions("pims:health_basic:export")
    @Log(title = "健康基础数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HealthBasic healthBasic)
    {
        List<HealthBasic> list = healthBasicService.selectHealthBasicList(healthBasic);
        ExcelUtil<HealthBasic> util = new ExcelUtil<HealthBasic>(HealthBasic.class);
        return util.exportExcel(list, "健康基础数据数据");
    }

    /**
     * 新增健康基础数据
     */
    @RequiresPermissions("pims:health_basic:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存健康基础数据
     */
    @RequiresPermissions("pims:health_basic:add")
    @Log(title = "健康基础数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HealthBasic healthBasic)
    {
        return toAjax(healthBasicService.insertHealthBasic(healthBasic));
    }

    /**
     * 修改健康基础数据
     */
    @RequiresPermissions("pims:health_basic:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        HealthBasic healthBasic = healthBasicService.selectHealthBasicById(id);
        mmap.put("healthBasic", healthBasic);
        return prefix + "/edit";
    }

    /**
     * 修改保存健康基础数据
     */
    @RequiresPermissions("pims:health_basic:edit")
    @Log(title = "健康基础数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HealthBasic healthBasic)
    {
        return toAjax(healthBasicService.updateHealthBasic(healthBasic));
    }

    /**
     * 删除健康基础数据
     */
    @RequiresPermissions("pims:health_basic:remove")
    @Log(title = "健康基础数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(healthBasicService.deleteHealthBasicByIds(ids));
    }
}
